/**
 * routes/deviceBorrow.js
 */
var express = require("express");
var router = express.Router();
//导入所需控制器
const deviceBorrow = require("../controller/borrowController");
const auth = require("../controller/auth");


/**
 * @swagger
 * components:
 *   schemas:
 *     DeviceBorrow:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *         deviceId:
 *           type: integer
 *         userId:
 *           type: integer
 *         borrowDate:
 *           type: string
 *           format: date-time
 *         returnDate:
 *           type: string
 *           format: date-time
 *           nullable: true
 *         status:
 *           type: string
 *           enum: [borrowed, returned]
 *         createdAt:
 *           type: string
 *           format: date-time
 *         updatedAt:
 *           type: string
 *           format: date-time
 */

/**
 * @swagger
 * tags:
 *   name: DeviceBorrows
 *   description: 设备借还管理
 */

/**
 * @swagger
 * /deuse/borrows:
 *   post:
 *     summary: 借用设备
 *     tags: [DeviceBorrows]
 *     description: 借用设备，需要设备ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - deviceId
 *             properties:
 *               deviceId:
 *                 type: integer
 *     responses:
 *       201:
 *         description: 借用成功，返回借用记录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/DeviceBorrow'
 *       400:
 *         description: 设备已借出或请求参数错误
 *       404:
 *         description: 设备不存在
 *       500:
 *         description: 服务器错误
 */
router.post("/borrows", auth.isLogin,deviceBorrow.add);

/**
 * @swagger
 * /deuse/borrows/return:
 *   put:
 *     summary: 归还设备
 *     tags: [DeviceBorrows]
 *     description: 归还设备
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - deviceId
 *             properties:
 *               deviceId:
 *                 type: integer
 *     responses:
 *       200:
 *         description: 归还成功，返回借用记录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/DeviceBorrow'
 *       400:
 *         description: 设备已归还或归还失败
 *       404:
 *         description: 借用记录不存在
 *       500:
 *         description: 服务器错误
 */
router.put("/borrows/return",auth.isLogin, deviceBorrow.update);

/**
 * @swagger
 * /deuse/borrows:
 *   get:
 *     summary: 获取借用记录列表
 *     tags: [DeviceBorrows]
 *     description: 获取所有借用记录列表
 *     parameters:
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [borrowed, returned]
 *         description: 过滤借用状态
 *       - in: query
 *         name: userId
 *         schema:
 *           type: integer
 *         description: 按用户ID过滤
 *       - in: query
 *         name: deviceId
 *         schema:
 *           type: integer
 *         description: 按设备ID过滤
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页条数
 *     responses:
 *       200:
 *         description: 返回借用记录列表（分页）
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                 page:
 *                   type: integer
 *                 pageSize:
 *                   type: integer
 *                 records:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/DeviceBorrow'
 *       500:
 *         description: 服务器错误
 */
router.get("/borrows",auth.isLogin,auth.isAdmin, deviceBorrow.findAll);

module.exports = router;
